package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.hunt.HuntBox;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HuntBoxDaoImpl extends TempDataObjectDao<HuntBox> {

    public Map<String, HuntBox> getHuntBoxMap(long startTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_hunt_box where createTime>?";
        Map<String, HuntBox> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, startTime);
            rs = call.executeQuery();
            while (rs.next()) {
                HuntBox data = parse(rs);
                dataMap.put(data.getHuntBoxUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(HuntBox data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_hunt_box(huntBoxUid,userId,createTime,goodsBoxId,openUserIdList) values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getHuntBoxUid());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getCreateTime());
            call.setInt(i++, data.getGoodsBoxId());
            call.setString(i++, data.getOpenUserIdListString());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(HuntBox data) {
        PreparedStatement call = null;
        String sql = "update t_u_hunt_box set userId=?,createTime=?,goodsBoxId=?,openUserIdList=? where huntBoxUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getCreateTime());
            call.setInt(i++, data.getGoodsBoxId());
            call.setString(i++, data.getOpenUserIdListString());
            call.setString(i++, data.getHuntBoxUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected HuntBox parseData(ResultSet rs) throws SQLException {
        HuntBox data = new HuntBox();
        data.setHuntBoxUid(rs.getString("huntBoxUid"));
        data.setUserId(rs.getLong("userId"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setGoodsBoxId(rs.getInt("goodsBoxId"));
        data.setOpenUserIdList(StringUtils.stringToLongList(rs.getString("openUserIdList"), ","));
        return data;
    }
}
